package com.lucenly.card.fragment;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lucenly.card.R;
import com.lucenly.card.activity.WebViewActivity;
import com.lucenly.card.adapter.Rank_tab_Adapter;
import com.lucenly.card.base.BaseFragment;
import com.lucenly.card.bean.Banner;
import com.lucenly.card.constants.Constants;
import com.lucenly.card.event.StringMsgEvent;
import com.lucenly.card.utils.PublicUtil;
import com.lucenly.card.view.refresh2.RefreshLayout;
import com.lucenly.card.view.refresh2.SmartRefreshLayout;
import com.lucenly.card.view.refresh2.listener.OnRefreshListener;
import com.vise.xsnow.event.IEvent;
import com.vise.xsnow.event.Subscribe;
import com.vise.xsnow.http.callback.ACallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class RankFragment extends BaseFragment{
    @BindView(R.id.tab_FindFragment_title)
    TabLayout tab_FindFragment_title;
    @BindView(R.id.vp_FindFragment_pager)
    ViewPager vp_FindFragment_pager;
    @BindView(R.id.iv_img)
    SimpleDraweeView iv_img;
    @BindView(R.id.ll_back)
    LinearLayout ll_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.smartLayout)
    SmartRefreshLayout smartLayout;
    private List<Fragment> list_fragment;                                //定义要装fragment的列表
    private List<String> list_title;
    Rank_tab_Adapter fAdapter;
    RankAFragment rankAFragment;
    RankBFragment rankBFragment;
    @Override
    protected int getLayoutID() {
        return R.layout.fragment_rank;
    }

    @Override
    protected void initView(View contentView) {
        tv_title.setText("打卡排行");
        ll_back.setVisibility(View.GONE);
        //将fragment装进列表中
        list_fragment = new ArrayList<>();
        rankAFragment = new RankAFragment();
        rankBFragment = new RankBFragment();
        list_fragment.add(rankAFragment);
        list_fragment.add(rankBFragment);
        list_title = new ArrayList<>();
        list_title.add("奖励榜");
        list_title.add("邀请榜");
        //为TabLayout添加tab名称
        tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(0)));
        tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(1)));

        fAdapter = new Rank_tab_Adapter(getChildFragmentManager(),list_fragment,list_title);

        //viewpager加载adapter
        vp_FindFragment_pager.setAdapter(fAdapter);
        //tab_FindFragment_title.setViewPager(vp_FindFragment_pager);
        //TabLayout加载viewpager
        tab_FindFragment_title.setupWithViewPager(vp_FindFragment_pager);

    }

    @Override
    public boolean isRegisterEvent() {
        return true;
    }
    @Subscribe
    public void refresh(IEvent iEvent){
        if(iEvent!=null&&iEvent instanceof StringMsgEvent){
            if(((StringMsgEvent) iEvent).getMsg().equals("RankFragment")){
                smartLayout.finishRefresh();
            }else if(((StringMsgEvent) iEvent).getMsg().equals("tab")){
                rankAFragment.setTop();
                rankBFragment.setTop();
                tab_FindFragment_title.getTabAt(0).select();
            }
        }
    }
    @Override
    protected void bindEvent() {
        getBanner();
        smartLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if(tab_FindFragment_title.getSelectedTabPosition()==0){
                    rankAFragment.refresh();
                }else{
                    rankBFragment.refresh();
                }
                getBanner();
            }
        });
        tab_FindFragment_title.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if(tab.getPosition()==0){
                    rankAFragment.refresh();
                }else{
                    rankBFragment.refresh();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    @Override
    protected void initData() {
        getBanner();
    }
    public void getBanner(){
        PublicUtil.getBanner(Constants.BANNER_RANK, new ACallback<Banner>() {
            @Override
            public void onSuccess(final Banner data) {
                iv_img.setImageURI(data.images);
//                GlideUtils.getInstance(getActivity()).load(getActivity(),data.images,iv_img,R.color.gray_de);
                iv_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(data==null){
                            return;
                        }
                        //Banner跳转
                        Intent intent = new Intent(mContext, WebViewActivity.class);
                        intent.putExtra("type",1);
                        intent.putExtra("path",data.url);
                        mContext.startActivity(intent);
                    }
                });
            }

            @Override
            public void onFail(int errCode, String errMsg) {

            }
        });
    }
    @Override
    protected void lazyLoad() {

    }

    @Override
    protected void processClick(View view) {

    }
}
